package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.PmsBrand;
import com.itheima.mapper.PmsBrandMapper;
import com.itheima.service.db.PmsBrandService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsBrandServiceImpl implements PmsBrandService {
    @Autowired
    PmsBrandMapper pmsBrandMapper;

    /**
     * 根据品牌名称分页获取品牌列表
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String keyword) {
        //1.构建分页查询器
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        //2.构建条件查询器
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        //3.添加关键字模糊查询
        lqw.like(StrUtil.isNotEmpty(keyword), PmsBrand::getName, keyword);
        lqw.orderByDesc(PmsBrand::getSort);
        //4.查询数据库
        Page<PmsBrand> brandPage = pmsBrandMapper.selectPage(page, lqw);
        //5.返回分页对象
        return new PageBeanVo(pageNum, pageSize, brandPage.getTotal(), brandPage.getRecords());

    }

    /**
     * 添加品牌信息
     *
     * @param pmsBrand
     * @return
     */
    @Override
    public Boolean addBrand(PmsBrand pmsBrand) {
        //1.首先根据品牌名称判断该品牌是否已经存在
        LambdaQueryWrapper<PmsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(PmsBrand::getName, pmsBrand.getName());
        Integer count = pmsBrandMapper.selectCount(qw);
        if (count > 0) {
            //该品牌已存在
            return false;
        } else {
            pmsBrandMapper.insert(pmsBrand);
            return true;
        }
    }

    /**
     * 批量删除品牌
     *
     * @param ids
     */
    @Override
    public void deleteBrands(List<Long> ids) {
        pmsBrandMapper.deleteBatchIds(ids);
    }

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @Override
    public List<PmsBrand> findAllBrand() {
        return pmsBrandMapper.selectList(null);
    }

    /**
     * 批量更新厂家制造商状态
     *
     * @param factoryStatus
     * @param ids
     */
    @Override
    public void updateFactoryStatus(Integer factoryStatus, List<Long> ids) {
        //遍历集合
        for (Long id : ids) {
            PmsBrand pmsBrand = new PmsBrand();
            pmsBrand.setId(id);
            pmsBrand.setFactoryStatus(factoryStatus);
            //根据id更新
            pmsBrandMapper.updateById(pmsBrand);
        }
    }

    /**
     * 批量更新显示状态
     *
     * @param showStatus
     * @param ids
     */
    @Override
    public void updateShowStatus(Integer showStatus, List<Long> ids) {
        //遍历集合
        for (Long id : ids) {
            PmsBrand pmsBrand = new PmsBrand();
            pmsBrand.setId(id);
            pmsBrand.setShowStatus(showStatus);
            //根据id更新
            pmsBrandMapper.updateById(pmsBrand);
        }
    }

    /**
     * 更新品牌
     *
     * @param pmsBrandParam
     * @return
     */
    @Override
    public Boolean updateBrandById(PmsBrand pmsBrandParam) {
        //首先根据名称查询
        LambdaQueryWrapper<PmsBrand> qw = new LambdaQueryWrapper<>();
        qw.eq(PmsBrand::getName, pmsBrandParam.getName());
        List<PmsBrand> brands = pmsBrandMapper.selectList(qw);
        if (CollUtil.isNotEmpty(brands)) {
            //如果有同名的就遍历进行判断
            for (PmsBrand brand : brands) {
                //如果同名的品牌id和传递的id也是一致的就直接更新
                if (brand.getId() == pmsBrandParam.getId()) {
                    pmsBrandMapper.updateById(pmsBrandParam);
                    return true;
                } else {
                    //否则就表示该品牌名称已存在
                    return false;
                }
            }
        }
        //如果没有同名的,就可以直接更新
        pmsBrandMapper.updateById(pmsBrandParam);
        return true;
    }

    /**
     * 根据编号查询品牌信息
     * @param id
     * @return
     */
    @Override
    public PmsBrand findBrandById(Long id) {
        return pmsBrandMapper.selectById(id);
    }
}
